package org.jeecg.modules.kc.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.ObjectUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.kc.entity.*;
import org.jeecg.modules.kc.enums.StorageEnum;
import org.jeecg.modules.kc.service.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.rooms.entity.CesGoods;
import org.jeecg.modules.rooms.entity.CesGoodsUnit;
import org.jeecg.modules.rooms.service.CesGoodsServiceImpl;
import org.jeecg.modules.rooms.service.CesGoodsUnitServiceImpl;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: kc_stock_detail
 * @Author: jeecg-boot
 * @Date:   2023-04-18
 * @Version: V1.0
 */
@Api(tags="kc_stock_detail")
@RestController
@RequestMapping("/kc/kcStockDetail")
@Slf4j
public class KcStockDetailController extends JeecgController<KcStockDetail, IKcStockDetailService> {
	@Autowired
	private IKcStockDetailService kcStockDetailService;
	@Autowired
	private IKcGoodsService kcGoodsService;
	@Autowired
	private CesGoodsUnitServiceImpl cesGoodsUnitService;
	@Autowired
	private IKcSupplierInGoodsService kcSupplierInGoodsService;
	@Autowired
	private IKcStockService kcStockService;
	@Autowired
	private  IKcDepositoryInGoodsService kcDepositoryInGoodsService;
	 @Autowired
	 private CesGoodsServiceImpl cesGoodsService;
	/**
	 * 分页列表查询
	 *
	 * @param kcStockDetail
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "kc_stock_detail-分页列表查询")
	@ApiOperation(value="kc_stock_detail-分页列表查询", notes="kc_stock_detail-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<KcStockDetail>> queryPageList(KcStockDetail kcStockDetail,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<KcStockDetail> queryWrapper = QueryGenerator.initQueryWrapper(kcStockDetail, req.getParameterMap());
		Page<KcStockDetail> page = new Page<KcStockDetail>(pageNo, pageSize);
		IPage<KcStockDetail> pageList = kcStockDetailService.page(page, queryWrapper);
		KcStock kcStock = null;
		if (ObjectUtils.isNotEmpty(kcStockDetail.getStockId())) {
			LambdaQueryWrapper<KcStock> lambdaQueryWrapper = new LambdaQueryWrapper<>();
			lambdaQueryWrapper.eq(KcStock::getCode, kcStockDetail.getStockId());
			kcStock = kcStockService.getOne(lambdaQueryWrapper);
		}
		for (KcStockDetail record : pageList.getRecords()) {
			if (kcStock != null) {
				if (kcStock.getType().equals(1)||kcStock.getType().equals(3)) {
					KcDepositoryInGoods kcDepositoryInGoods = kcDepositoryInGoodsService.getById(record.getSupplierInGoodsId());
					if (kcDepositoryInGoods != null) {
						record.setStockNum(kcDepositoryInGoods.getNum());
						CesGoods cesGoods = cesGoodsService.getById(kcDepositoryInGoods.getGoodsId());
						if (cesGoods != null) {
							record.setSpec(cesGoods.getSpec());
							record.setName(cesGoods.getName());
							CesGoodsUnit cesGoodsUnit = cesGoodsUnitService.getById(cesGoods.getGoodUnit());
							if (cesGoodsUnit != null) {
								record.setGoodUnit(cesGoodsUnit.getName());
							}
						}
					}
				} else {
					KcSupplierInGoods kcSupplierInGoods = kcSupplierInGoodsService.getById(record.getSupplierInGoodsId());
					if (kcSupplierInGoods != null) {
						CesGoods cesGoods = cesGoodsService.getById(kcSupplierInGoods.getGoodsId());
						if (cesGoods != null) {
							record.setSpec(cesGoods.getSpec());
							record.setName(cesGoods.getName());
							CesGoodsUnit cesGoodsUnit = cesGoodsUnitService.getById(cesGoods.getGoodUnit());
							if (cesGoodsUnit != null) {
								record.setGoodUnit(cesGoodsUnit.getName());
							}
						}
					}
				}
			}
		}
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param kcStockDetail
	 * @return
	 */
	@AutoLog(value = "kc_stock_detail-添加")
	@ApiOperation(value="kc_stock_detail-添加", notes="kc_stock_detail-添加")
	//@RequiresPermissions("kc:kc_stock_detail:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody KcStockDetail kcStockDetail) {
		kcStockDetailService.save(kcStockDetail);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param kcStockDetail
	 * @return
	 */
	@AutoLog(value = "kc_stock_detail-编辑")
	@ApiOperation(value="kc_stock_detail-编辑", notes="kc_stock_detail-编辑")
	//@RequiresPermissions("kc:kc_stock_detail:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody KcStockDetail kcStockDetail) {
		kcStockDetailService.updateById(kcStockDetail);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "kc_stock_detail-通过id删除")
	@ApiOperation(value="kc_stock_detail-通过id删除", notes="kc_stock_detail-通过id删除")
	//@RequiresPermissions("kc:kc_stock_detail:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		kcStockDetailService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "kc_stock_detail-批量删除")
	@ApiOperation(value="kc_stock_detail-批量删除", notes="kc_stock_detail-批量删除")
	//@RequiresPermissions("kc:kc_stock_detail:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.kcStockDetailService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "kc_stock_detail-通过id查询")
	@ApiOperation(value="kc_stock_detail-通过id查询", notes="kc_stock_detail-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<KcStockDetail> queryById(@RequestParam(name="id",required=true) String id) {
		KcStockDetail kcStockDetail = kcStockDetailService.getById(id);
		if(kcStockDetail==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(kcStockDetail);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param kcStockDetail
    */
    //@RequiresPermissions("kc:kc_stock_detail:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, KcStockDetail kcStockDetail) {
        return super.exportXls(request, kcStockDetail, KcStockDetail.class, "kc_stock_detail");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("kc:kc_stock_detail:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, KcStockDetail.class);
    }

}
